/* @(#)GSOrderSubmitForNonFlatQueryAction.java   
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-3-6     新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSProviderBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.dao.GSProviderDAO;
import com.hitrust.gs.form.GSOrderSubmitForNonFlatForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSOrderSubmitForNonFlatVO;

/**
 * 查询网下订单明细
 */
public class GSOrderSubmitForNonFlatQueryAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        
        GSOrderSubmitForNonFlatForm tForm = (GSOrderSubmitForNonFlatForm)aForm;
        //取得页面查询参数
        GSOrderSubmitForNonFlatVO tOrderSubForNonFlatVO = new GSOrderSubmitForNonFlatVO();
//        tOrderSubForNonFlatVO.orderId = tForm.orderId;
        tOrderSubForNonFlatVO.orderDetailId = tForm.orderDetailId;
        tOrderSubForNonFlatVO.providerName = tForm.providerName;
        tOrderSubForNonFlatVO.orderDeptId = tForm.orderDeptId;
        tOrderSubForNonFlatVO.deliverGoodsAddr = tForm.deliverGoodsAddrId;
        tOrderSubForNonFlatVO.orderDetailStatus = tForm.orderDetailStatus;
        tOrderSubForNonFlatVO.concentricityId = tForm.concentricityId;
        tOrderSubForNonFlatVO.manufInfo = tForm.manufInfo;
        tOrderSubForNonFlatVO.manufType1 = tForm.manuftype1;
        tOrderSubForNonFlatVO.manufType2 = tForm.manuftype2;
        tOrderSubForNonFlatVO.manufType3 = tForm.manuftype3;
        //格式化查询日期
        String[] tDateStr = new String[2];
        tDateStr[0] = tForm.orderCreateDateStr;
        tDateStr[1] = tForm.orderCreateDateEnd;
        tDateStr = DateUtil.formateDate(tDateStr);
        tOrderSubForNonFlatVO.orderCreateDateStr = tDateStr[0]; 
        tOrderSubForNonFlatVO.orderCreateDateEnd = tDateStr[1];
        //取得当前操作员可查询的订货单位和送货地址
        GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        GSOrderDeptInfoProcessor tProc = new GSOrderDeptInfoProcessor();
        HashMap tMap = tProc.getOrderDeptInfoOfBusOper(tOperBean.getGs_oper_org_id());
        aForm.getRequest().setAttribute("orderdept",tMap);
        ArrayList tOrg = (ArrayList)tMap.get("organization");
        //取得产品分类
        GSManufactureProcessor tManufProc = new GSManufactureProcessor();
        tManufProc.getManufactureOfLevel(aForm);
        //取得分页参数
        String[] tStr = this.getPageParams(tForm,tOrderSubForNonFlatVO,tOrg);
        //执行查询
        GSOrderProcessor tOrderProc = new GSOrderProcessor();
        ArrayList tList = tOrderProc.getOrderDetailForNonFlatProvider(tOrderSubForNonFlatVO,tOrg,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
        ResultProcess tRsProc = new ResultProcess();
        tRsProc.setResult(tList);
        tForm.getRequest().setAttribute("order",tRsProc);
        
        if(tForm.providerName!= null && !tForm.orderDeptId.equals("0000")){
            GSProviderDAO tProvDAO  = new GSProviderDAO();
            GSProviderBean tProvBean = tProvDAO.queryProviderByName(tForm.providerName);
            tForm.getRequest().setAttribute("provider",tProvBean);
        }
        //存储分页参数
        tForm.getRequest().setAttribute("pageSize",tStr[1]);
        tForm.getRequest().setAttribute("totalRecords",tStr[2]);
        return GSBaseConstants.FORWARD_SUCCESS;
    }
    private String[] getPageParams(GSBaseForm aForm,GSOrderSubmitForNonFlatVO aOrderVO,ArrayList aOrg) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            GSOrderDetailDAO tDAO = new GSOrderDetailDAO();
            totalRecords = tDAO.queryOrderCountForNonFlatProv(aOrderVO,aOrg);
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }

}
